package com.vehiclemanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehiclemanagement.entity.OperationLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 操作日志数据访问接口
 */
@Mapper
public interface OperationLogMapper extends BaseMapper<OperationLog> {

    /**
     * 分页查询日志
     *
     * @param page 分页参数
     * @param whereSql 条件SQL
     * @return 分页结果
     */
    @Select("SELECT * FROM operation_log ${whereSql} ORDER BY create_time DESC")
    IPage<OperationLog> selectPageLogs(Page<OperationLog> page, @Param("whereSql") String whereSql);

    /**
     * 统计用户操作次数
     *
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 操作次数
     */
    @Select("SELECT COUNT(*) FROM operation_log WHERE user_id = #{userId} AND create_time BETWEEN #{startTime} AND #{endTime}")
    int countUserOperations(@Param("userId") Long userId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 按操作类型统计
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计结果
     */
    @Select("SELECT operation_type, COUNT(*) as count FROM operation_log " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY operation_type ORDER BY count DESC")
    List<Map<String, Object>> statisticsByOperationType(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 按模块统计
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计结果
     */
    @Select("SELECT module, COUNT(*) as count FROM operation_log " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY module ORDER BY count DESC")
    List<Map<String, Object>> statisticsByModule(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

    /**
     * 按日期统计
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计结果
     */
    @Select("SELECT DATE(create_time) as date, COUNT(*) as count FROM operation_log " +
            "WHERE create_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY DATE(create_time) ORDER BY date")
    List<Map<String, Object>> statisticsByDate(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
} 